package com.hongxun.budget.ui.dialog;

import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.hongxun.budget.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 从底部弹出的dialog父类
 */
public abstract class BaseBottomFragmentDialog extends DialogFragment {
    protected View rootView;
    protected LayoutInflater inflater;
    protected Context context;
    Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.inflater = inflater;
        context = getActivity();
        rootView = inflater.inflate(getLayoutId(), null);
        unbinder = ButterKnife.bind(this, rootView);
        initViews();
        return rootView;
    }

    protected abstract int getLayoutId();

    @Override
    public void onResume() {
        getDialog().getWindow().setWindowAnimations(R.style.dialogStyle);
        getDialog().getWindow().setGravity(Gravity.BOTTOM);
//        ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();//设置最高高度
//        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                int height = rootView.getHeight();
//                FrameLayout.LayoutParams layoutParams;
//                if (height > ScreenUtils.getScreenHeight(getActivity()) / 8 * 7) {
//                    layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ScreenUtils.getScreenHeight(getActivity()) / 8 * 7);
//                } else {
//                    layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
//                }
//                rootView.setLayoutParams(layoutParams);
//            }
//        });
        super.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
        getDialog().getWindow().setBackgroundDrawableResource(R.color.transparent);
    }

    protected abstract void initViews();
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
